/**
 * Created by jinkailong on 2019/3/6.
 */

#ifndef AUDIODECODER_AUDIORESAMPLE_H
#define AUDIODECODER_AUDIORESAMPLE_H

#include <string.h>
#include <memory.h>

#ifdef __cplusplus
extern "C"
{
#endif
#include <libavutil/opt.h>
#include <libavutil/channel_layout.h>
#include <libavutil/samplefmt.h>
#include <libswresample/swresample.h>
#ifdef __cplusplus
}
#endif

class AudioResample {
public:
    AudioResample() {
        swr_ctx = nullptr;
        m_bRunning = false;
        m_pSrcData = nullptr;
        m_pDstData = nullptr;
    }

    virtual ~AudioResample() {

    }

    int initResample(int inChannels, int inSampleRate,
                     int outChannels, int outSampleRate);

    int8_t *resample(int8_t *buf, int bufLen, int *dst_bufsize);

    void end();

private:
    void release();

private:
    bool m_bRunning;

    int m_nSrcChannels;
    int m_nSrcSampleRate;

    int m_nDstChannels;
    int m_nDstSampleRate;

    int m_nMaxDstNumSamples;

    uint8_t **m_pSrcData;
    uint8_t **m_pDstData;

    struct SwrContext *swr_ctx;
};


#endif //AUDIODECODER_AUDIORESAMPLE_H
